$(function() {
    new Mdate("date_box", {
        //"dateShowBtn"为你点击触发Mdate的id，必填项
        acceptId: "date_box",
        //此项为你要显示选择后的日期的input，不填写默认为上一行的"dateShowBtn"
        beginYear: "2015",
        //此项为Mdate的初始年份，不填写默认为2000
        beginMonth: "1",
        //此项为Mdate的初始月份，不填写默认为1
        beginDay: "1",
        //此项为Mdate的初始日期，不填写默认为1
        //此项为Mdate的结束年份，不填写默认为当年
        //此项为Mdate的结束月份，不填写默认为当月
        //此项为Mdate的结束日期，不填写默认为当天
        format: "-"
        //此项为Mdate需要显示的格式，可填写"/"或"-"或".",不填写默认为年月日
    });
    let months = [];
    let current_month = '';
    let current_combo_uuid = '';
    let start_month = '';
    alert_choose_date = () => {
        $('.tips').css('display', 'block');
        setTimeout(() => {
            $('.tips').css('display', 'none');
        },5000)
    };
    search = () => {
        let birthday = $('#date_box').val();
        console.log(birthday)
        $.ajax({
                type: "GET",
                timeout: 15000,
                url: 'https://kksappapi.kekeshi.com/api/app/v1/mommy_school/combo_orders/preview_choose_combo?platform=android&birthday=' + birthday,
                dataType: "json",
                data: {},
                success: function(response) {
                    if (response.code === 200) {
                    var data = response.data;
                    var str = '';
                    let eq = -1;
                    start_month = data.start_month;
                    current_combo_uuid = data.combos[0].uuid;
                    current_month = data.start_month;
                    // 宝宝当前月
                        $('.current_month').eq(0).html(start_month);
                    // 注意：输出的i与obj分别为下标与单个的对象
                    $.each(data.combos, function(i, obj) {
                        str += '<div class="one_year" data-uuid="' + obj.uuid + '" onclick="render_page(this)">';
                        str += '<div class="top_title_price">';
                        str += '<div class="course_title">' + obj.title + '</div>';
                        str += '</div>';
                        if (obj.courses.length !== 0) {
                            str += '<div class="center_content_box">';
                            $.each(obj.courses, function (n, course_obj) {
                                str += '<div class="content_box">';
                                str += '<div class="content_title">' + course_obj.name + '</div>';
                                str += '<div class="content_number">' + course_obj.count + '节</div>';
                                str += '</div>';
                            });
                            str += '<div style="clear:both;"></div>';
                            str += '</div>';
                            str += '<div class="course_desc">共计' + obj.course_count + '节课</div>';
                        }
                        str += '</div>';
                        str += '</div>';
                        if (obj.choose === true) {
                            eq = i;
                        }
                    });
                    $(".course").html(str);
                    $('.one_year').eq(eq).css('border','1px solid #ff7d7d');
                    $('.one_year').eq(eq).css('background','#FFF2F2');
                    $('.center_content_box').eq(eq).css('background','#ffffff');
                    $('.center_content_box').eq(eq).find('.content_number').css('background','#fff2f2');
                    $('.course_desc').eq(eq).css('color','#ff7d7d');

                    // 月份
                    months = [];
                    $('.month').css('background', 'none');
                    $('.month').css('color', '#333333');
                    $.each(data.month_table, function(i, obj) {
                        if (obj.state === 1) {
                            $('.month').eq(i).css('background','#ffc2c2');
                            $('.month').eq(i).css('color','#ffffff');
                        } else if (obj.state === 2) {
                            $('.month').eq(i).css('background','#ff7d7d');
                            $('.month').eq(i).css('color','#ffffff');
                            months.push(i + 1);
                        }
                    });

                    if (data.start_month <= 36) {
                        return false;
                    } else {
                        $('.center_content_box').css('display','none');
                        $('.course_desc').css('display','none');
                    }
                    if (data.alert != "") {
                        $('.alert_bg').css('display','block');
                        $('.alert_text').html(data.alert);
                    }
                    this.clicked = false;
                } else if (response.code === 401) {
                        $('.error_bg').css('display','block');
                        $('.error_text').html(response.msg);
                    }
                },
                complete: function (XMLHttpRequest,status) {
                    console.log(status)
                    if (status == 'timeout') {
                        $('.alert_text').html('请求已超时');
                        $('.alert_bg').css('display','block');
                    }
                }
            });
    };
    $('.month').on('click',function(){
        current_month = $(this).text();
        console.log(current_month);
        if ($('#date_box').val() === '') {
            $('.tips').css('display', 'block');
            setTimeout(() => {
                $('.tips').css('display', 'none');
            },5000)
            return false;
        }
        $.ajax({
            type: "GET",
            timeout: 15000,
            url: 'https://kksappapi.kekeshi.com/api/app/v1/mommy_school/combo_orders/preview_choose_combo?platform=android&month=' + current_month + '&combo_uuid=' + current_combo_uuid,
            dataType: "json",
            data: {},
            success: function(response) {
                console.log(response.data);
                if (response.code === 200) {
                    var data = response.data;
                    var str = '';
                    let eq = -1;
                    start_month = data.start_month;
                    // 注意：输出的i与obj分别为下标与单个的对象
                    $.each(data.combos, function (i, obj) {
                        str += '<div class="one_year" data-uuid="' + obj.uuid + '" onclick="render_page(this)">';
                        str += '<div class="top_title_price">';
                        str += '<div class="course_title">' + obj.title + '</div>';
                        str += '</div>';
                        if (obj.courses.length !== 0) {
                            str += '<div class="center_content_box">';
                            $.each(obj.courses, function (n, course_obj) {
                                str += '<div class="content_box">';
                                str += '<div class="content_title">' + course_obj.name + '</div>';
                                str += '<div class="content_number">' + course_obj.count + '节</div>';
                                str += '</div>';
                            });
                            str += '<div style="clear:both;"></div>';
                            str += '</div>';
                            str += '<div class="course_desc">共计' + obj.course_count + '节课</div>';
                        }
                        str += '</div>';
                        str += '</div>';
                        if (obj.choose === true) {
                            eq = i;
                        }
                    });
                    $(".course").html(str);
                    $('.one_year').eq(eq).css('border', '1px solid #ff7d7d');
                    $('.one_year').eq(eq).css('background', '#FFF2F2');
                    $('.center_content_box').eq(eq).css('background', '#ffffff');
                    $('.center_content_box').eq(eq).find('.content_number').css('background', '#fff2f2');
                    $('.course_desc').eq(eq).css('color', '#ff7d7d');

                    // 月份
                    months = [];
                    $('.month').css('background', 'none')
                    $('.month').css('color', '#333333')
                    $.each(data.month_table, function (i, obj) {
                        if (obj.state === 1) {
                            $('.month').eq(i).css('background', '#ffc2c2');
                            $('.month').eq(i).css('color', '#ffffff');
                        } else if (obj.state === 2) {
                            $('.month').eq(i).css('background', '#ff7d7d');
                            $('.month').eq(i).css('color', '#ffffff');
                            months.push(i + 1);
                        }
                    });
                    if (data.alert != "") {
                        $('.alert_bg').css('display', 'block');
                        $('.alert_text').html(data.alert);
                    }
                    this.clicked = false;
                } else if (response.code === 401) {
                    $('.error_bg').css('display','block');
                    $('.error_text').html(response.msg);
                }
            },
            complete: function (XMLHttpRequest,status) {
                console.log(status)
            if (status == 'timeout') {
                $('.alert_text').html('请求已超时');
                $('.alert_bg').css('display','block');
            }
        }
        });
    });
    render_page = function(e) {
        current_combo_uuid = $(e).attr('data-uuid');
        $.ajax({
            type: "GET",
            timeout: 15000,
            url: 'https://kksappapi.kekeshi.com/api/app/v1/mommy_school/combo_orders/preview_choose_combo?platform=android&month=' + current_month + '&combo_uuid=' + current_combo_uuid,
            dataType: "json",
            data: {},
            success: function(response) {
                console.log(response.data);
                if(response.code === 200) {
                var data = response.data;
                var str = '';
                let eq = -1;
                start_month = data.start_month;
                // 注意：输出的i与obj分别为下标与单个的对象
                    $.each(data.combos, function (i, obj) {
                        str += '<div class="one_year" data-uuid="' + obj.uuid + '" onclick="render_page(this)">';
                        str += '<div class="top_title_price">';
                        str += '<div class="course_title">' + obj.title + '</div>';
                        str += '</div>';
                        if (obj.courses.length !== 0) {
                            str += '<div class="center_content_box">';
                            $.each(obj.courses, function (n, course_obj) {
                                str += '<div class="content_box">';
                                str += '<div class="content_title">' + course_obj.name + '</div>';
                                str += '<div class="content_number">' + course_obj.count + '节</div>';
                                str += '</div>';
                            });
                            str += '<div style="clear:both;"></div>';
                            str += '</div>';
                            str += '<div class="course_desc">共计' + obj.course_count + '节课</div>';
                        }
                        str += '</div>';
                        str += '</div>';
                        if (obj.choose === true) {
                            eq = i;
                        }
                    });
                $(".course").html(str);
                $('.one_year').eq(eq).css('border','1px solid #ff7d7d');
                $('.one_year').eq(eq).css('background','#FFF2F2');
                $('.center_content_box').eq(eq).css('background','#ffffff');
                $('.center_content_box').eq(eq).find('.content_number').css('background','#fff2f2');
                $('.course_desc').eq(eq).css('color','#ff7d7d');

                // 月份
                months = [];
                $('.month').css('background','none');
                $('.month').css('color','#333333');
                $.each(data.month_table, function(i, obj) {
                    if (obj.state === 1) {
                        $('.month').eq(i).css('background','#ffc2c2');
                        $('.month').eq(i).css('color','#ffffff');
                    } else if (obj.state === 2) {
                        $('.month').eq(i).css('background','#ff7d7d');
                        $('.month').eq(i).css('color','#ffffff');
                        months.push(i + 1);
                    }
                });
                if (data.alert != "") {
                    $('.alert_bg').css('display','block');
                    $('.alert_text').html(data.alert);
                }
                this.clicked = false;
                } else if (response.code === 401) {
                    $('.error_bg').css('display','block');
                    $('.error_text').html(response.msg);
                }
            },
            complete: function (XMLHttpRequest,status) {
                console.log(status)
                if (status == 'timeout') {
                    $('.alert_text').html('请求已超时');
                    $('.alert_bg').css('display','block');
                }
            }
        });
    };
    close_alert = () => {
      $('.alert_bg').css('display','none');
      $('.alert_text').html('');
    }
});
